﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Subtle.Domain.Services.Interfaces;
using Subtle.Data.Persistence.Interfaces;
using Subtle.Domain.Models;

namespace Subtle.Domain.Services
{
	/// <summary>
	/// Blog Service
	/// </summary>
	public class BlogService : IBlogService
	{
		private IRepository repository;

		/// <summary>
		/// Initializes a new Blog Service
		/// </summary>
		/// <param name="blogUnitOfWork"></param>
		public BlogService(IRepository repository)
		{
			this.repository = repository;
		}

		/// <summary>
		/// Has Blogs
		/// </summary>
		public bool HasBlogs
		{
			get
			{
				var installedQuery = from blog in repository.Query<Blog>()
									 select blog;
				return installedQuery != null && installedQuery.Count() > 0;
			}
		}

		/// <summary>
		/// Setup Blog
		/// </summary>
		/// <param name="blog"></param>
		/// <returns></returns>
		public bool SetupBlog(Blog blog)
		{
			return repository.Save<Blog>(blog);
		}

		/// <summary>
		/// Get Blog
		/// </summary>
		/// <param name="blogTitle"></param>
		/// <returns></returns>
		public Models.Blog GetBlog(string blogTitle)
		{
			var blogQuery = from blog in repository.Query<Blog>()
							where blog.Title == blogTitle
							select blog;
			return blogQuery.FirstOrDefault();
		}

		/// <summary>
		/// Get Default Blog
		/// </summary>
		/// <param name="blogTitle"></param>
		/// <returns></returns>
		public Models.Blog GetDefaultBlog()
		{
			var blogQuery = from blog in repository.Query<Blog>()
							select blog;
			return blogQuery.FirstOrDefault();
		}
	}
}
